﻿@inject DialogService DialogService
@inject ProductService ProductService
@inject ProductUnitService ProductUnitService

<RadzenStack>
    <RadzenTemplateForm EditContext="editContext" TItem="Product" Data="@model" Submit=@OnSubmit>
        <RadzenFieldset Text="商品信息">
            <RadzenStack Gap="1rem">
                <RadzenStack class="rz-p-0" 
                                Orientation="Orientation.Vertical" 
                                AlignItems="AlignItems.Normal"
                                JustifyContent="JustifyContent.SpaceBetween">
                    <RadzenRow Gap="1rem">
                        <RadzenColumn Size="12" SizeMD="12">
                            <RadzenStack>
                                <RadzenFormField Text="名称" Style="width: 100%;">
                                    <ChildContent>
                                        <RadzenTextBox Name="Name" @bind-Value="@model!.Name" />
                                        <RadzenRequiredValidator Component="Name" Text="名称不能为空。" Popup=true Style="position: absolute" />
                                    </ChildContent>
                                    <Helper>
                                        <RadzenText TextStyle="TextStyle.Caption" Style="color:red">
                                            * 必填
                                        </RadzenText>
                                    </Helper>
                                </RadzenFormField>
                            </RadzenStack>
                        </RadzenColumn>
                    </RadzenRow>
                    <RadzenRow Gap="1rem">
                        <RadzenColumn Size="12" SizeMD="12">
                            <RadzenStack>
                                <RadzenFormField Text="代码" Style="width: 100%;">
                                    <ChildContent>
                                        <RadzenTextBox @bind-Value="@model!.Code" />
                                    </ChildContent>
                                </RadzenFormField>
                            </RadzenStack>
                        </RadzenColumn>
                    </RadzenRow>

                    <RadzenRow Gap="1rem">
                        <RadzenColumn>
                                <RadzenStack Style="display:flex" Orientation="Orientation.Horizontal">
                                    <RadzenLabel Style="min-width: fit-content; align-items: center" Text="单位" Component="DropDownBindValue" />
                                    <RadzenDropDown @bind-Value=@model!.Unit Data=@unitNames Style="width: 100%;" Name="DropDownBindValue" />
                                </RadzenStack>
                        </RadzenColumn>
                    </RadzenRow>
                    
                    <RadzenRow Gap="1rem">
                        <RadzenColumn Size="12" SizeMD="12">
                            <RadzenStack>
                                <RadzenFormField Text="备注" Style="width: 100%;">
                                    <ChildContent>
                                        <RadzenTextBox @bind-Value="@model!.Remark" />
                                    </ChildContent>
                                </RadzenFormField>
                            </RadzenStack>
                        </RadzenColumn>
                    </RadzenRow>

                </RadzenStack>
                <RadzenStack Gap="1rem" Orientation="Orientation.Horizontal" JustifyContent="JustifyContent.End" Style="height: 20%;">
                    <RadzenButton ButtonStyle="ButtonStyle.Secondary" Text="取消" Click="OnCancel" />
                    <RadzenButton ButtonStyle="ButtonStyle.Primary" Text="保存" ButtonType="ButtonType.Submit" />
                </RadzenStack>
            </RadzenStack>
        </RadzenFieldset>
    </RadzenTemplateForm>
</RadzenStack>

@code {
    [Parameter] public Product? PProduct { get; set; }
    [Parameter] public long PProductTypeId { get; set; }

    Product model = default!;
    EditContext? editContext;
    
    string? unitName = string.Empty;
    IEnumerable<string> unitNames = default!;

    void OnSubmit()
    {
        try
        {
            ProductService.Save(model);
            DialogService.Close(model);
        }
        catch (Exception e)
        {
            DialogService.Alert(e.Message, "错误", new AlertOptions() { OkButtonText = "确定" });
        }
    }

    private void OnCancel()
    {
        ProductService.ResetOriginalData();
        DialogService.Close(null);
    }

    protected override async Task OnInitializedAsync()
    {
        await Task.Run(() =>
        {
            var unitList = ProductUnitService.Search(new BaseInput());
            unitNames = unitList.Select(s=>s.Name);
            unitName = unitNames.FirstOrDefault();

            model = PProduct!;
            editContext = new EditContext(model);
        });
    }
}

